package com.qdaily.ui.ui.views.QDWebView;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.webkit.WebView;

import com.qdaily.supportlibrary.util.QLog;
import com.qdaily.ui.ui.views.QDWebView.QDArticleWebViewFileCache.QDArticleWebViewFileCacheMgr;

import java.io.IOException;
import java.util.Scanner;

/**
 * Created by song on 15/5/4.
 */
public class QDArticleWebView extends WebView{

    //Handler开头，用于被js调用
    public final static String HandlerKey_ChildComment = "qdaily::comment";
    public final static String HandlerKey_ChildPraise = "qdaily::commentPraise";
    public final static String HandlerKey_PicPreview = "qdaily::picsPreview";
    public final static String HandlerKey_GoPage = "qdaily::gotoPage";
    public final static String HandlerKey_PageReady = "qdaily::pageReady";

//    callback开头，用于调用js
    public final static String CallBackKey_ChildCommentResult = "qdaily::commentSuccess";
    public final static String CallBackKey_ChildPraiseResult = "qdaily::commentPraiseSuccess";
    public final static String CallBackKey_SetFont = "qdaily::setFontSize";

    private WebViewJavascriptBridge bridge;

    /**
     * Comments for <code>TAG</code>
     * TAG
     */
    private String TAG = "QDArticleWebView";

    /**
     * QDArticleWebview 构造器
     *
     * @param context
     * @param attrs
     */
    public QDArticleWebView(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    /**
     * QDArticleWebview 构造器
     *
     * @param context
     */
    public QDArticleWebView(Context context) {
        super(context);

    }

    public void loadArticle(String url){
        if (!url.startsWith("http"))
        {
            url = "http://" + url;
        }
        url = url + QDArticleWebViewFileCacheMgr.QDailyArticleExt;
        loadUrl(url);
    }

    public void configJavascriptBidge(Activity activity)
    {
        bridge = new WebViewJavascriptBridge(activity, this, new WebViewJavascriptBridge.WVJBHandler() {
            @Override
            public void handle(String data, WebViewJavascriptBridge.WVJBResponseCallback jsCallback) {
                QLog.w(TAG, "WebViewJavascriptBridge 默认func request:" + data);
            }
        });
    }

    public WebViewJavascriptBridge getBridge() {
        return bridge;
    }

    public void setBridge(WebViewJavascriptBridge bridge) {
        this.bridge = bridge;
    }

    public void javascriptBridgeInjectComplete()
    {
    }

    /**
     * 计算纵向范围，用于滚动到底部
     *
     * (non-Javadoc)
     * [url=home.php?mod=space&uid=133757]@see[/url] android.webkit.WebView#computeVerticalScrollRange()
     */
    @Override
    public int computeVerticalScrollRange() {
//        int computeVerticalScrollRange = super.computeVerticalScrollRange();
//        Log.i(TAG, "computeVerticalScrollRange" + computeVerticalScrollRange);
        return super.computeVerticalScrollRange();
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
    }
}
